﻿using AutoMapper;
using MediatR;
using SheepStorage.API.Application.Command.UserInfoCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.API.Filter;
using SheepStorage.Domain.Entity;
using SheepStorage.Service.Interface;
using SheepStorage.API.Filter;
using SheepStorage.Domain.DTO.Output;
namespace SheepStorage.API.Application.CommandHandlers.UserInfoHandlers
{
    public class UserInfoLoginHandlers : IRequestHandler<UserInfoLoginCommand, Results<UserInfoOutput>>
    {

        private readonly IUserInfoService userInfoService;
        private readonly IMapper mapper;
        private readonly TokenService _tokenService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userInfoService"></param>
        /// <param name="mapper"></param>
        public UserInfoLoginHandlers(IUserInfoService userInfoService, IMapper mapper, TokenService _tokenService)
        {
            this.userInfoService = userInfoService;
            this.mapper = mapper;
            this._tokenService = _tokenService;
        }

        //用户登录
        public async Task<Results<UserInfoOutput>> Handle(UserInfoLoginCommand request, CancellationToken cancellationToken)
        {


            var userentity =await userInfoService.UserLogin(request.UserName, request.UserPassWord);
            if (userentity != null)
            {
                var token = _tokenService.GenerateToken(userentity);
                return Results<UserInfoOutput>.TokenResult(token, userentity);

            }

		
         return Results<UserInfoOutput>.FailResult("用户或者密码不正确");


        }

    }
}
